import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: window
    width: 400
    height: 300
    visible: true
    title: "QML-C++交互示例"

    // 消息框
    Rectangle {
        id: messageBox
        width: parent.width - 40
        height: 40
        color: "#eee"
        radius: 5
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.horizontalCenter: parent.horizontalCenter
        visible: false

        Text {
            anchors.centerIn: parent
            text: messageText.text
        }

        Timer {
            id: hideTimer
            interval: 2000
            onTriggered: messageBox.visible = false
        }
    }

    // 消息文本
    QtObject {
        id: messageText
        property string text: ""
    }

    // 主内容
    Column {
        anchors.centerIn: parent
        spacing: 20

        Text {
            text: "当前计数: " + counter.count
            font.pixelSize: 24
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Row {
            spacing: 10
            anchors.horizontalCenter: parent.horizontalCenter

            Button {
                text: counter.running ? "暂停" : "开始"
                onClicked: counter.running ? counter.stop() : counter.start()
            }

            Button {
                text: "重置"
                onClicked: counter.reset()
            }
        }
    }

    // 连接C++信号
    Connections {
        target: counter
        onShowMessage: {
            messageText.text = message
            messageBox.visible = true
            hideTimer.restart()
        }
    }
}